package com.nsoft.html5npub.business;

import com.nsoft.html5npub.R;
import com.nsoft.html5npub.objects.BookRange;
import com.nsoft.html5npub.objects.Settings;
import com.nsoft.html5npub.utils.BookListAdapter;
import com.nsoft.html5npub.utils.Constant;
import com.nsoft.html5npub.utils.CoverFlow;
import com.nsoft.html5npub.utils.BookNewListAdapter;
import com.nsoft.html5npub.utils.engine;
import com.nsoft.html5npub.utils.utils;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.widget.ListView;

/**
 * this class handles the synchronization with server or
 * local list at device.
 * <br>
 * the class will contact the <b>SyncBookThread</b> to 
 * perform the synchronization.
 * 
 * @author minhld
 *
 */
public class ServerSyncEngine implements 
			View.OnClickListener,BookSyncListener {
	
	private Context context;
	private ListView list;
	private CoverFlow newList;
	
	public ServerSyncEngine(Context ctx,
			ListView _list,CoverFlow _nList){
		this.context=ctx;
		this.list=_list;
		this.newList=_nList;
	}
	
	@Override
	public void onClick(View v) {
		AlertDialog.Builder alert=
				new AlertDialog.Builder(
						this.context);
			alert.setIcon(R.drawable.ic_help);
			alert.setTitle(Constant.DIALOG_CONFIRM).
				setMessage(Constant.
						booklist.BLF_SERVER_SYNC_CONFIRM).
				setPositiveButton(Constant.DIALOG_YES,
					new DialogInterface.OnClickListener(){
						@Override
						public void onClick(
								DialogInterface arg0, 
								int arg1){
							getBookList(SyncBookThread.
									SYNC_SOURCE_SERVER);
						}
					
				}).setNegativeButton(Constant.DIALOG_NO,
					new DialogInterface.OnClickListener() {
						@Override
						public void onClick(
								DialogInterface dialog,
								int which){
							dialog.cancel();
						}
				}).create().show();
		
	}
	
	public void getBookList(int syncSrc){
		if (syncSrc==SyncBookThread.SYNC_SOURCE_SERVER){
			if (!utils.isOnline(this.context)){
				utils.showError(this.context,
					Constant.dataserver.ERROR_NETWORK_TITLE,
					Constant.dataserver.
						ERROR_NETWORK_UNAVAILABLE);
				return;
			}
		}
		
    	engine.setEngineServer(
    			Constant.dataserver.SERVER_PROTOCOL+
    			Settings.getServerName()+
    			Constant.PATH_SPLITTER);
    	String downloadUrl=Settings.getDownloadUrl();
    	if (Settings.isUseAppId()){
    		downloadUrl+=Constant.CHR_AND+
    				Constant.settingtags.APPID_PARAM+
    				Constant.CHR_EQUAL+
    				Settings.getAppId();
    	}
    	engine.setEngineParams(downloadUrl);

    	ProgressDialog pd=ProgressDialog.show(context,
				Constant.booklist.BLF_SERVER_UPDATE,
				syncSrc==SyncBookThread.SYNC_SOURCE_SERVER? 
					Constant.booklist.BLF_SERVER_SYNCHRONIZE:
					Constant.booklist.BLF_BOOKLIST_SYNCHRONIZE,
				true,false);
	
    	SyncBookThread sbt=new SyncBookThread(
    						context,pd,syncSrc);
    	sbt.setBookSyncListener(this);
    	sbt.start();
    }
	
	@Override
	public void synchronizeDone(){
		/**
		Bitmap[] thumbBmps=engine.
				getBookThumbBitmapsFromHistory();
		
		list.setAdapter(new BookArrayAdapter(
				context,thumbBmps,engine.getBookList()));
		*/
		list.setAdapter(new BookListAdapter(
					context,engine.getBookList()));
		
		/**
		Bitmap[] subList=thumbBmps.length>=
				Constant.newbooks.NEW_BOOK_NUMBER?
				Arrays.copyOfRange(thumbBmps,0,
					Constant.newbooks.NEW_BOOK_NUMBER):
				thumbBmps;
				
		int height=newList.getLayoutParams().height;
		newList.setAdapter(new NewBookAdapter(
					this.context,subList,height));
		*/
		int numOfBooks=engine.getBookNumber();
		int numNewBooks=Constant.newbooks.NEW_BOOK_NUMBER;
		BookRange bookRange=new BookRange();
		bookRange.startIndex=0;
		bookRange.endIndex=numOfBooks>numNewBooks?
						numNewBooks:numOfBooks;
		
		int height=newList.getLayoutParams().height;
		newList.setAdapter(new BookNewListAdapter(
				this.context,bookRange,height));		
	}
}
